package com.tommy.jiami.ui;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.ActionMode;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ListView;

import com.tommy.jiami.R;
import com.tommy.jiami.adapter.PassageAdapter;
import com.tommy.jiami.db.DBHelper;
import com.tommy.jiami.ui.dialog.AddPassageDialog;
import com.tommy.jiami.ui.dialog.LockDialog;
import com.tommy.jiami.utils.Constants;
import com.tommy.jiami.utils.Utils;

public class PassageActivity extends AppCompatActivity
{
	DBHelper helper;
	PassageAdapter adapter;
	ListView listView;
	Cursor c;
	BroadcastReceiver receiver=new BroadcastReceiver(){
		@Override
		public void onReceive(Context context, Intent intent)
		{
			c=helper.getReadableDatabase().rawQuery("select * from passage",null);
			adapter.changeCursor(c);
		}
	};
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_list);
		Toolbar toolbar=(Toolbar)findViewById(R.id.toolbar) ;
		setSupportActionBar(toolbar);
		helper=new DBHelper(this,"tommydb",1);
		getListView().setDividerHeight(0);
		c=helper.getReadableDatabase().rawQuery("select * from passage",null);
		adapter=new PassageAdapter(this,c);
		getListView().setAdapter(adapter);
		getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
		getListView().setMultiChoiceModeListener(new MultiChoiceListener());
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		getMenuInflater().inflate(R.menu.menu_passage,menu);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item)
	{
		switch(item.getItemId()){
			case R.id.passage_menu_add:
				AddPassageDialog dialog=new AddPassageDialog();
				dialog.show(getFragmentManager(),"dialog");
				break;
		}
		return super.onOptionsItemSelected(item);
	}

	
	class MultiChoiceListener implements AbsListView.MultiChoiceModeListener
	{

		@Override
		public boolean onCreateActionMode(ActionMode action, Menu menu)
		{
			getMenuInflater().inflate(R.menu.action_passage,menu);
			return true;
		}

		@Override
		public boolean onPrepareActionMode(ActionMode action, Menu menu)
		{
			return false;
		}

		@Override
		public boolean onActionItemClicked(ActionMode action, MenuItem item)
		{
			switch(item.getItemId()){
				case R.id.passage_action_delete:
					for(long position:getListView().getCheckedItemIds())
					{
						deletePassage((int)position);	
					}
					action.finish();
					break;
			}
			return false;
		}

		@Override
		public void onDestroyActionMode(ActionMode action)
		{
		}

		@Override
		public void onItemCheckedStateChanged(ActionMode p1, int p2, long p3, boolean p4)
		{
		}
				
	}
	
	private void deletePassage(int position)
	{
		try{
		helper.getWritableDatabase().execSQL("delete from passage where _id=?",new Object[]{position});
		sendBroadcast(new Intent(Constants.ACTION_UPDATE_PASSAGE));
		}catch(Exception e){
			Utils.showToast("错误："+e);
		}
	}

	@Override
	protected void onStart()
	{
		IntentFilter filter=new IntentFilter();
		filter.addAction(Constants.ACTION_UPDATE_PASSAGE);
		registerReceiver(receiver,filter);
		super.onStart();
	}

	@Override
	protected void onStop()
	{
		unregisterReceiver(receiver);
		super.onStop();
	}

	public ListView getListView() {
		if(listView==null) {
			listView = (ListView) findViewById(R.id.list_view);
			listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
				@Override
				public void onItemClick(AdapterView<?> parent, View view, int position,final long id) {
					LockDialog dialog=new LockDialog();
					dialog.setOnLockSuccessListener(new LockDialog.OnLockSuccessListener(){
						@Override
						public void onSuccess()
						{
							new AddPassageDialog((int)id).show(getFragmentManager(),"dialog");
						}
					});
					dialog.show(getFragmentManager(),"dialog");
				}
			});
		}
		return listView;
	}
	
}
